<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
	<TITLE></TITLE>
	<META NAME="GENERATOR" CONTENT="OpenOffice.org 3.2  (Unix)">
	<META NAME="CREATED" CONTENT="0;0">
	<META NAME="CHANGED" CONTENT="20110101;14194600">
	<STYLE TYPE="text/css">
	<!--
		@page { margin: 0.79in }
		P { margin-bottom: 0.08in }
		PRE { font-family: "Times New Roman" }
	-->
	</STYLE>
</HEAD>
<BODY LANG="el-GR" DIR="LTR">
<PRE LANG="en-US"><FONT SIZE=5><B>conjgrad</B></FONT>


<FONT SIZE=4><SPAN LANG="en-US"><SPAN STYLE="font-style: normal">Solves the system of equations Ax=b, where A is an nXn positive definite symmetric matrix</SPAN><SPAN STYLE="font-style: normal">, </SPAN><SPAN STYLE="font-style: normal">by the method of conjugate gradients,</SPAN></SPAN></FONT>
<FONT SIZE=4><SPAN LANG="en-US"><SPAN STYLE="font-style: normal">for</SPAN> details see book of Hang T. Lau, “A Java Numerical Library for Scientists and engineers|”, Chapman &amp; Hall, 2004</SPAN></FONT>

<FONT SIZE=4><SPAN LANG="en-US"><B>Example: Can be run with F5 within the ScalaLab Editor (Ctrl-E to open the editor)</B></SPAN></FONT>



<SPAN LANG="en-US">import java.text.DecimalFormat;</SPAN>
<SPAN LANG="en-US">import numal.*;</SPAN>

<SPAN LANG="en-US">public class Test_conjgrad extends Object implements LA_conjgrad_methods {</SPAN>
<SPAN LANG="en-US">	</SPAN>
<SPAN LANG="en-US">	public static void main(String args[]) {</SPAN>

    <SPAN LANG="en-US">int i;</SPAN>
    <SPAN LANG="en-US">int it[] = new int[1];</SPAN>
    <SPAN LANG="en-US">double no[] = new double[1];</SPAN>
    <SPAN LANG="en-US">double x[] = new double[13];</SPAN>
    <SPAN LANG="en-US">double b[] = new double[13];</SPAN>

    <SPAN LANG="en-US">DecimalFormat fiveDigit = new DecimalFormat(&quot;0.00000E0&quot;);</SPAN>
    <SPAN LANG="en-US">for (i=0; i&lt;=12; i++) x[i]=b[i]=0.0;</SPAN>
    <SPAN LANG="en-US">b[0]=1.0;  b[12]=4.0;</SPAN>
    <SPAN LANG="en-US">Test_conjgrad testconjgrad = new Test_conjgrad();;</SPAN>
    <SPAN LANG="en-US">Linear_algebra.conjgrad(testconjgrad,x,b,0,12,it,no);</SPAN>
    <SPAN LANG="en-US">System.out.println(&quot;Delivers:\n  IT = &quot; + it[0] + &quot;,    NO = &quot; + </SPAN>
               <SPAN LANG="en-US">fiveDigit.format(no[0]) + &quot;\n\n      X          R\n&quot;);</SPAN>
    <SPAN LANG="en-US">for (i=0; i&lt;=12; i++)</SPAN>
      <SPAN LANG="en-US">System.out.println(&quot;  &quot; + fiveDigit.format(x[i]) + &quot;   &quot; + </SPAN>
                         <SPAN LANG="en-US">fiveDigit.format(b[i]));</SPAN>
<SPAN LANG="en-US">	}</SPAN>
<SPAN LANG="en-US">	</SPAN>
  <SPAN LANG="en-US">public void matvec(double x[], double b[]) {</SPAN>
    
    <SPAN LANG="en-US">int i;</SPAN>

    <SPAN LANG="en-US">b[0]=2.0*x[0]-x[1];</SPAN>
    <SPAN LANG="en-US">for (i=1; i&lt;=11; i++) b[i] = -x[i-1]+2.0*x[i]-x[i+1];</SPAN>
    <SPAN LANG="en-US">b[12]=2.0*x[12]-x[11];</SPAN>
  <SPAN LANG="en-US">}</SPAN>
  
  <SPAN LANG="en-US">public boolean goon(int iter[], double norm[]) {    </SPAN>
    <SPAN LANG="en-US">return (iter[0]&lt;20 &amp;&amp; norm[0]&gt;1.0e-10);</SPAN>
  <SPAN LANG="en-US">}</SPAN>
<SPAN LANG="en-US">}</SPAN></PRE>
</BODY>
</HTML>